<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * @package	Lib
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */
define("LIB_DIR", "./lib/");
define("CONTROLLER_DIR", "./app/controller/");

/* some constats for the framework */
define("FMW_URL_ABOUT", "http://code.google.com/p/phpkiisf/");
define("FMW_AUTHOR_ABOUT", "http://mcezzare.blogspot.com.br/");
define("INCLUDE_EXTENSION", ".inc.php");

/**
 * Arquivo de configurações do aplicativo
 */
final class config {

    public static function init() {
	self::_load();
    }

    /** @var array config data */
    private static $data = null;

    public function __construct() {
	call_user_func(array($this, "_load"));
    }

    /**
     * @return array
     * @throws Exception
     */
    public static function get_config($section = null) {
	if ($section === null) {
	    return self::get_data();
	}
	$data = self::get_data();
	if (!array_key_exists($section, $data)) {
	    throw new Exception('Unknown config section: ' . $section);
	}
	return $data[$section];
    }

    /**
     * @return array
     */
    private static function get_data() {
	if (self::$data !== null) {
	    return self::$data;
	}
	self::$data = parse_ini_file('phpkiisf.ini', true);
	return self::$data;
    }

    /**
     *
     * @param type $options
     */
    public static function display_config($options = null) {

	display_user_message(sprintf("Hello from : %s .<br>Displaying config from .ini.", get_class()));
	return self::read_config(false);
    }

    /**
     * Initialize constants
     */
    private static function _load($initialize_constants = true) {
	self::read_config($initialize_constants);
    }

    /**
     * Read the .ini file and create the constants.
     *
     * @param boolean $initialize_constants for only reading  and not output the values
     *
     * Values are stored in this format
      $return = array(
      "SECTION" => array(
      "CONSTANT" => "VALUE" ,
      ) ,
      );
     * Inside the loop foreach SECTION is $key
     * Inside the loop while
     * $param = constant
     * $val = value
     */
    public static function read_config($initialize_constants = false) {
	$return = array();
	foreach (self::get_data() as $key => $value) {
	    $section = array();
	    while (list( $param, $val ) = each($value)) {
		array_push($section, array($param => $val));
		//define constant
		if ($initialize_constants) {
		    if (!defined($param)) {
			define(strtoupper("$param"), $val);
		    }
		}
	    }
	    array_push($return, array($key => $section));
	}
	/* remove empty value */
//				unset($return[0]);
	return $return;
    }

}

?>
